package com.th.supcom.pds.config.server;

import java.util.List;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.util.PyWbUtil;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.config.client.model.PhamMachineConfigModel;
import com.th.supcom.pds.config.client.service.PhamMachineConfigRpc;
import com.th.supcom.pds.domain.pojo.PhamMachineConfig;
import com.th.supcom.pds.internal.api.config.PhamMachineConfigService;

/**
 * PhamMachineConfig 控制层.
 * 
 * 药房机器属性配置.
 */
public class PhamMachineConfigServiceController extends GwtController implements PhamMachineConfigRpc
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamMachineConfigServiceController.class);

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private static final long serialVersionUID = 1L;

    private PhamMachineConfigService m_phamMachineConfigService;

    @Override
    public void addPhamMachineConfig (final PhamMachineConfigModel phamMachineConfigModel) throws UiException
    {
        PhamMachineConfigServiceController.s_logger.debug ("call addPhamMachineConfig (PhamMachineConfigModel phamMachineConfigModel=【{}】)", phamMachineConfigModel);
        final PhamMachineConfig phamMachineConfig = ConvertUtils.convert (PhamMachineConfig.class, phamMachineConfigModel);
        try
        {
            m_phamMachineConfigService.addPhamMachineConfig (phamMachineConfig);
        }
        catch (final Exception e)
        {
            PhamMachineConfigServiceController.s_logger.error (e.getMessage (), e);
            throw new UiException (e.getMessage (), e);
        }
    }

    @Override
    public List <PhamMachineConfigModel> findPhamMachineConfigByExample (final PhamMachineConfigModel model) throws UiException
    {
        PhamMachineConfigServiceController.s_logger.debug ("call findPhamMachineConfigByExample (PhamMachineConfigModel phamMachineConfigModel=【{}】)", model);
        final PhamMachineConfig phamMachineConfig = ConvertUtils.convert (PhamMachineConfig.class, model);
        try
        {
            final List <PhamMachineConfig> list = m_phamMachineConfigService.findPhamMachineConfigByExample (phamMachineConfig);
            final List <PhamMachineConfigModel> modelList = ConvertUtils.convert (PhamMachineConfigModel.class, list);
            return modelList;
        }
        catch (final Exception e)
        {
            PhamMachineConfigServiceController.s_logger.error (e.getMessage (), e);
            throw new UiException (e.getMessage (), e);
        }
    }

    @Override
    public PhamMachineConfigModel getPhamMachineConfigById (final Long id) throws UiException
    {
        PhamMachineConfigServiceController.s_logger.debug ("call getPhamMachineConfigById (Long id=【{}】)", id);
        try
        {
            final PhamMachineConfig phamMachineConfig = m_phamMachineConfigService.getPhamMachineConfigById (id);
            final PhamMachineConfigModel model = ConvertUtils.convert (PhamMachineConfigModel.class, phamMachineConfig);
            return model;
        }
        catch (final Exception e)
        {
            PhamMachineConfigServiceController.s_logger.error (e.getMessage (), e);
            throw new UiException (e.getMessage (), e);
        }
    }

    @Override
    public String getPinYinCode (final String value)
    {
        String resultString = "";
        if (value != null && !("".equals (value)))
        {
            resultString = PyWbUtil.getPyCode (value);
        }
        return resultString;
    }

    @Override
    public void modifyPhamMachineConfig (final PhamMachineConfigModel phamMachineConfigModel) throws UiException
    {
        PhamMachineConfigServiceController.s_logger.debug ("call modifyPhamMachineConfig (PhamMachineConfigModel phamMachineConfigModel=【{}】)", phamMachineConfigModel);
        final PhamMachineConfig phamMachineConfig = ConvertUtils.convert (PhamMachineConfig.class, phamMachineConfigModel);
        try
        {
            m_phamMachineConfigService.modifyPhamMachineConfig (phamMachineConfig);
        }
        catch (final Exception e)
        {
            PhamMachineConfigServiceController.s_logger.error (e.getMessage (), e);
            throw new UiException (e.getMessage (), e);
        }
    }

    @Override
    public void modifyPhamMachineConfigByExample (final PhamMachineConfigModel phamMachineConfigModel) throws UiException
    {
        PhamMachineConfigServiceController.s_logger.debug ("call modifyPhamMachineConfigByExample (PhamMachineConfigModel phamMachineConfigModel=【{}】)", phamMachineConfigModel);
        final PhamMachineConfig phamMachineConfig = ConvertUtils.convert (PhamMachineConfig.class, phamMachineConfigModel);
        try
        {
            m_phamMachineConfigService.modifyPhamMachineConfigByExample (phamMachineConfig);
        }
        catch (final Exception e)
        {
            PhamMachineConfigServiceController.s_logger.error (e.getMessage (), e);
            throw new UiException (e.getMessage (), e);
        }
    }

    @Override
    public void removePhamMachineConfig (final Long id) throws UiException
    {
        PhamMachineConfigServiceController.s_logger.debug ("call removePhamMachineConfig (Long id=【{}】)", id);
        final PhamMachineConfig phamMachineConfig = new PhamMachineConfig ();
        phamMachineConfig.setPhamMachineConfigId (id);
        try
        {
            m_phamMachineConfigService.removePhamMachineConfig (phamMachineConfig);
        }
        catch (final Exception e)
        {
            PhamMachineConfigServiceController.s_logger.error (e.getMessage (), e);
            throw new UiException (e.getMessage (), e);
        }
    }

    public void setPhamMachineConfigService (final PhamMachineConfigService phamMachineConfigService)
    {
        m_phamMachineConfigService = phamMachineConfigService;
    }
}